/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.logistics.web.outboundorder;

import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.warehouse.Twarehouse;
import com.jeesite.modules.base.service.cars.TcarService;
import com.jeesite.modules.base.service.warehouse.TwarehouseService;
import com.jeesite.modules.logistics.entity.inboundorder.InboundOrder;
import com.jeesite.modules.logistics.entity.outboundorder.OutboundOrder;
import com.jeesite.modules.logistics.service.inboundorder.InboundOrderService;
import com.jeesite.modules.logistics.service.outboundorder.OutboundOrderService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.service.EmpUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 出库单Controller
 * @author LV
 * @version 2021-03-10
 */
@Controller
@RequestMapping(value = "${adminPath}/logistics/outboundorder/outboundOrder")
public class OutboundOrderController extends BaseController {

	@Autowired
	private OutboundOrderService outboundOrderService;
    @Autowired
    private ServiceOrderService serviceOrderService;
    @Autowired
    private TwarehouseService warehouseService;
    @Autowired
    private InboundOrderService inboundOrderService;
    @Autowired
    private EmpUserService empUserService;
    @Autowired
    private TcarService carService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public OutboundOrder get(String outboundOrderId, boolean isNewRecord) {
		return outboundOrderService.get(outboundOrderId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("logistics:outboundorder:outboundOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(OutboundOrder outboundOrder, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));
        model.addAttribute("listWarehouse", warehouseService.findList(new Twarehouse()));
        model.addAttribute("listInboundOrder", inboundOrderService.findList(new InboundOrder()));
        model.addAttribute("listCar", carService.findList(new Tcar()));
        model.addAttribute("listUser", empUserService.findList(new EmpUser()));
		model.addAttribute("outboundOrder", outboundOrder);
		return "modules/logistics/outboundorder/outboundOrderList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("logistics:outboundorder:outboundOrder:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<OutboundOrder> listData(OutboundOrder outboundOrder, HttpServletRequest request, HttpServletResponse response) {
		outboundOrder.setPage(new Page<>(request, response));
		Page<OutboundOrder> page = outboundOrderService.findPage(outboundOrder);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("logistics:outboundorder:outboundOrder:view")
	@RequestMapping(value = "form")
	public String form(OutboundOrder outboundOrder, Model model) {
        if (outboundOrder.getIsNewRecord()){
            model.addAttribute("listFrameNumber", ListUtils.newArrayList());
        }else {
            model.addAttribute("listFrameNumber", serviceOrderService.get(outboundOrder.getServiceOrderId()).getServiceOrderDetailList());
        }
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));
        model.addAttribute("listWarehouse", warehouseService.findList(new Twarehouse()));
        model.addAttribute("listInboundOrder", inboundOrderService.findList(new InboundOrder()));
        model.addAttribute("listCar", carService.findList(new Tcar()));
		model.addAttribute("outboundOrder", outboundOrder);
		return "modules/logistics/outboundorder/outboundOrderForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("logistics:outboundorder:outboundOrder:view")
	@RequestMapping(value = "detail")
	public String detail(OutboundOrder outboundOrder, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));
        model.addAttribute("listWarehouse", warehouseService.findList(new Twarehouse()));
        model.addAttribute("listInboundOrder", inboundOrderService.findList(new InboundOrder()));
        model.addAttribute("listCar", carService.findList(new Tcar()));
        model.addAttribute("outboundOrder", outboundOrder);
		return "modules/logistics/outboundorder/outboundOrderDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("logistics:outboundorder:outboundOrder:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated OutboundOrder outboundOrder) {
		outboundOrderService.save(outboundOrder);
		return renderResult(Global.TRUE, text("保存出库单成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("logistics:outboundorder:outboundOrder:delete")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(OutboundOrder outboundOrder) {
		outboundOrderService.delete(outboundOrder);
		return renderResult(Global.TRUE, text("删除出库单成功！"));
	}
	
	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("logistics:outboundorder:outboundOrder:view")
	@RequestMapping(value = "outboundOrderSelect")
	public String empUserSelect(OutboundOrder outboundOrder, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("OutboundOrder", outboundOrder);
		return "modules/logistics/outboundorder/outboundOrderSelect";
	}
	
}